<?php
//-------------Create by Giang 5/10/2012---------------------
require 'src/facebook.php';
require '../../../../../config.php'; 
require_once(DIR_SYSTEM . 'database/mysql.php');
require_once (DIR_SYSTEM .'helper/customer.php');
require_once (DIR_SYSTEM.'engine/registry.php');
require_once (DIR_SYSTEM.'library/db.php');
require_once (DIR_SYSTEM.'library/session.php');

$facebook = new Facebook(array('appId'  => '448294291880853','secret' => '8cff92439ed443d45f21652c28df188b','cookie' => true,));
$session = $facebook->getSession();
$me = null;
if ($session) 
{
  try 
  {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } 
  catch (FacebookApiException $e) 
  {
    error_log($e);
  }
}

if ($me) {
	  $logoutUrl = $facebook->getLogoutUrl();
} 
else {
		$loginUrl = $facebook->getLoginUrl();
}

if ($me) {
		$session = new Session();
			Registry::set('session', $session);
		$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
			Registry::set('db', $db);
		$email = $me['email'];
		$username = $me['email'];
		$sql = $db->query("SELECT * FROM customer where LOWER(email) = '$email'");
		if(!$sql->num_rows){
			$db->query("INSERT INTO customer (email,status,ym,username) values('$email','1','nick_0_trung#@^!','$username')");
		}
		$login_face = new HelperCustomer();
		$login_face->login($email,'',true);
		header('Location: ' . HTTP_SERVER . 'index.php');
}
else {
		header('Location: '.$loginUrl); 
}
 ?>

